﻿using Customer.API.Applcation.Command.FinanceCommand;
using Customer.Infrstructre.FinanceManager;
using MediatR;

namespace Customer.API.Applcation.CommandHandlers.FinanceCommandHandler
{
    /// <summary>
    /// 删除财务类别
    /// </summary>
    public class DelFinanceCommandHandler:IRequestHandler<DelFinanceCommand,int>
    {
        private readonly IFinanceTypeRepository _financeRepository;

        public DelFinanceCommandHandler(IFinanceTypeRepository financeRepository)
        {
            _financeRepository = financeRepository;
        }

        public async Task<int> Handle(DelFinanceCommand request, CancellationToken cancellationToken)
        {
            var count = 0;
            foreach (var item in request.FinanceTypeId)
            {
                var res = (await _financeRepository.GetAllAsync()).FirstOrDefault(x => x.FinanceTypeId == item);
                if (res != null)
                {
                    res.IsDel = true;
                    res.DelName = request.DelName;
                    res.DelTime = request.DelTime;
                    await _financeRepository.Update(res);
                    count = 1;
                }
                else
                {
                    count = -1;
                }
            }
            return count;
        }
    }
}
